﻿using ArtMath.CommonType;
using ArtMath.Core.Helper;
using ArtMath.Resource;
using ArtMath.Setting;
using System;
using System.ComponentModel;
using System.Windows.Forms;
using WeifenLuo.WinFormsUI.Docking;

namespace ArtMath.Window
{
    public partial class DocumentWindow : DockContent
    {
        [Browsable(false)]
        public Document LinkedDocument { get; }
        [Browsable(false)]
        public DockState DockStateBackup { get; private set; }
        #region Menus
        [Browsable(false)]
        public ToolStripMenuItem SaveMenu
        {
            get { return tsmiSave; }
        }
        [Browsable(false)]
        public ToolStripMenuItem CloseMenu
        {
            get { return tsmiClose; }
        }
        [Browsable(false)]
        public ToolStripMenuItem CloseLeftMenu
        {
            get { return tsmiCloseLeft; }
        }
        [Browsable(false)]
        public ToolStripMenuItem CloseRightMenu
        {
            get { return tsmiCloseRight; }
        }
        [Browsable(false)]
        public ToolStripMenuItem CloseOtherMenu
        {
            get { return 关闭其他文档ToolStripMenuItem; }
        }
        [Browsable(false)]
        public ToolStripMenuItem CloseAllMenu
        {
            get { return 关闭所有文档AToolStripMenuItem; }
        }
        [Browsable(false)]
        public ToolStripMenuItem FloatAllMenu
        {
            get { return 全部浮动ToolStripMenuItem; }
        }
        #endregion
        public DocumentWindow(Document doc)
        {
            InitializeComponent();
            SetStyle(ControlStyles.UserPaint, true);
            SetStyle(ControlStyles.AllPaintingInWmPaint, true);
            SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
            SetStyle(ControlStyles.ResizeRedraw, true);
            UpdateStyles();
            LinkedDocument = doc;
            Text = LinkedDocument.Title;
            SaveMenu.Tag = CloseMenu.Tag = CloseOtherMenu.Tag = LinkedDocument;
            CloseLeftMenu.Tag = CloseRightMenu.Tag = this;
        }
        public void Document_TitleChanged(Document sender)
        {
            tsmiSave.Text = Resources.TranslateMenuItem("Save", 'S') + new string(' ', 1) + sender.Title;
        }
        public void SketchPad_ToolStripCreated(SketchPad sender, ToolStrip strip)
        {
            ChangeColorTheme(strip, Settings.Instance.ColorTheme);
        }
        public void ChangeColorTheme(ToolStrip strip, UIColorTheme theme)
        {
            switch (theme)
            {
                case UIColorTheme.Light:
                    EnableVSRenderer(strip, VisualStudioToolStripExtender.VsVersion.Vs2015, vS2015LightTheme1);
                    break;
                case UIColorTheme.Blue:
                    EnableVSRenderer(strip, VisualStudioToolStripExtender.VsVersion.Vs2015, vS2015BlueTheme1);
                    break;
                case UIColorTheme.Dark:
                    EnableVSRenderer(strip, VisualStudioToolStripExtender.VsVersion.Vs2015, vS2015DarkTheme1);
                    break;
            }
        }
        void EnableVSRenderer(ToolStrip strip, VisualStudioToolStripExtender.VsVersion version, ThemeBase theme)
        {
            visualStudioToolStripExtender1.SetStyle(strip, version, theme);
        }

        private void tsmiOpenFolder_Click(object sender, EventArgs e)
        {
            if (LinkedDocument.LinkedFile == null) return;
            Utility.ExploreFile(LinkedDocument.LinkedFile.Name);
        }

        private void 浮动FToolStripMenuItem_Click(object sender, EventArgs e)
        {
            DockState = DockState.Float;
        }
        private void DocumentWindow_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (e.CloseReason == CloseReason.UserClosing)
            {
                DockState = DockState.Hidden;
                e.Cancel = true;
            }
        }
        private void DocumentWindow_DockStateChanged(object sender, EventArgs e)
        {
            if (DockStateBackup == DockState.Unknown && DockState == DockState.Hidden) return;
            switch (DockState)
            {
                case DockState.Unknown:
                case DockState.Hidden:
                    LinkedDocument.DocumentsManagerItem.ImageIndex = 0;
                    ToolStripMenuItem menu = LinkedDocument.WindowMenuItem.OwnerItem as ToolStripMenuItem;
                    if (menu != null)
                    {
                        menu.DropDownItems.Remove(LinkedDocument.WindowMenuItem);
                    }
                    if (LinkedDocument.LinkedFile != null)
                    {
                        LinkedDocument.LinkedFile.Close();
                        LinkedDocument.LinkedFile = null;
                    }
                    break;
                default:
                    LinkedDocument.DocumentsManagerItem.ImageIndex = 1;
                    DockStateBackup = DockState;
                    break;
            }
        }

        private void DocumentWindow_Activated(object sender, EventArgs e)
        {
            if (!LinkedDocument.IsActivated)
            {
                LinkedDocument.Activate();
            }
        }

        private void DocumentWindow_Deactivate(object sender, EventArgs e)
        {
            if (LinkedDocument.IsActivated)
            {
                LinkedDocument.Deactivate();
            }
        }
    }
}
